﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Jack.Common
{
    //@jack mark string类型扩展
    public static class StringHelper
    {
        //@jack mark 获取开头的几位字符串
        public static string GetLeft(this string str, int Length)
        {
            if (!String.IsNullOrEmpty(str))
            {
                if (Length >= str.Length)
                    return str;
                else
                    return str.Substring(0, Length);
            }
            else
                return "";
        }

        //@jack mark int类型转换
        public static int ToInteger(this string str)
        {
            if (!String.IsNullOrEmpty(str))
            {
                str = Regex.Replace(str, "[^0-9^-]", string.Empty);
                if (str != "")
                {
                    str = str.GetLeft(1) + str.Substring(1).Replace('-', '\0');
                }
                int result;
                int.TryParse(str, out result);
                return result;
            }
            else
                return -1;
        }
    }
}
